//
//  ApiGuoKaiHepler.m
//  OO_CLASS
//
//  Created by KwokChun Cheung on 15/12/1.
//  Copyright © 2015年 eenet. All rights reserved.
//

#import "ApiGuoKaiHepler.h"
#import "ClassHelper.h"
#import "ApiContactManager.h"

@interface ApiGuoKaiHepler()
@property (nonatomic, strong) NSDictionary *paths;
@property (nonatomic, strong) NSString *PathPrefix;
@end

@implementation ApiGuoKaiHepler

-(instancetype)init{
    if (self = [super init]) {
        
    }
    return self;
}

-(void)dealloc{
    self.operation = nil;
}

-(void)setIsTest:(BOOL)isTest{
    _isTest = isTest;
    [ApiContactManager adjustApiPathTestStatus:isTest];
}

#pragma mark - Post Methods

-(NSString *)archiveWithKeyPath:(NSString *)keyPath{
    NSString *apiPath = [self.PathPrefix stringByAppendingString:self.paths[keyPath]];
    return apiPath;
}

-(BOOL)archiveIsTest{
    NSString *urlPath = self.paths[@"isTest"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:GKZYContentType];
    
    __weak __typeof(self) weakSelf = self;
    
    self.operation = [manager POST:urlPath parameters:nil success:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        weakSelf.isTest = NO;
    }];
    
    [self.operation start];
    [self.operation waitUntilFinished];
    NSString *isTestValue = [self.operation responseObject][@"isTest"];
    
    if (!isTestValue || [isTestValue isEqualToString:@""]) {
        return NO;
    }
    
    return [isTestValue isEqualToString:@"Y"];
}

-(NSMutableDictionary *)toPOST:(NSString *)keyPath
                 andParameters:(NSDictionary *)parameters
                andContentType:(NSString *)contentType
                       andSync:(BOOL)isSync
                    andSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))successBack
                    andFailure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failureBack{
    
    NSString *urlPath = [self archiveWithKeyPath:keyPath];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:contentType];
    
    self.operation = [manager POST:urlPath parameters:parameters success:successBack failure:failureBack];
    
    [self.operation start];
    if (isSync) {
        [self.operation waitUntilFinished];
    }
    return [self.operation responseObject];
    
}

-(NSMutableDictionary *)toPOST:(NSString *)keyPath
                        andReq:(BasicReq *)req
                andContentType:(NSString *)contentType
                       andSync:(BOOL)isSync
                    andSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))successBack
                    andFailure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failureBack {
    
    NSDictionary *parameters = [ClassHelper toDictionary:req];
    
    return [self toPOST:keyPath andParameters:parameters andContentType:contentType andSync:isSync andSuccess:successBack andFailure:failureBack];
}

#pragma mark - Getter For Property

-(NSDictionary *)paths{
    if (!_paths){
        _paths = ({
            NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"ApiGuoKaiPath" ofType:@"plist"];
            if (!plistPath) {
                NSLog(@"ApiGuoKaiPath PLIST NOT EXSITS!");
                return [NSDictionary new];
            }
            
            NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
            dictionary;
        });
    }
    return _paths;
}

-(NSString *)PathPrefix{
    if (self.isTest) {
        return self.paths[@"Prefix_tt"];
    }else{
        return self.paths[@"Prefix"];
    }
}

@end
